<?php

namespace Common\Behavior;

use Think\Behavior;
use Think\Hook;

defined('THINK_PATH') or exit();

/**
 * 初始化钩子信息
 * 参考了OneThink相关代码
 * @author ZXZ <zxz054321@163.com>
 */
class InitHookBehavior extends Behavior {

    // 行为扩展的执行入口必须是run
    public function run(&$content) {
        if (defined('BIND_MODULE') && BIND_MODULE === 'Install')
            return;

        $data = S('hooks');

        if (!$data) {
            #缓存不存在，读库取数据
            $hooks = D('ListenView')
                    ->where(array('status' => TRUE))
                    ->field('hook_name,addon_name')
                    ->select();

            foreach ($hooks as $hook) {
                Hook::add($hook['hook_name'], get_addon_class($hook['addon_name']));
            }

            //写入缓存
            S('hooks', Hook::get());
        }

        Hook::import($data, false);
    }

}
